/*
 * Copyright (c) 2021 Huize (Chengdu) Network Technology Co., LTD All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.asserts;

import com.huize.zhike.framework.common.enumeration.ErrorEnum;
import com.huize.zhike.framework.common.exception.BizException;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * 统一断言类
 *
 * @author tianyuliang
 * @version $Id: Assert.java, v0.1 2019/10/15
 */
public class Assert {

    public static void isTrue(boolean isTrue) {
        isTrue(isTrue, ErrorEnum.INVALID_PARAMS, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void isTrue(boolean isTrue, String msg) {
        isTrue(isTrue, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void isTrue(boolean isTrue, ErrorEnum errorEnum) {
        isTrue(isTrue, errorEnum, errorEnum.getDesc());
    }

    public static void isTrue(boolean isTrue, ErrorEnum errorEnum, String msg) {
        if (!isTrue) {
            if (StringUtils.isBlank(msg)) {
                throw new BizException(errorEnum);
            }
            throw new BizException(errorEnum, msg);
        }
    }

    public static void equals(long expected, long actual) {
        assertEquals(null, expected, actual);
    }

    public static void assertEquals(String message, long expected, long actual) {
        if (expected != actual) {
            failNotEquals(message, Long.valueOf(expected), Long.valueOf(actual));
        }
    }

    private static void failNotEquals(String message, Object expected, Object actual) {
        fail(format(message, expected, actual));
    }

    static public void fail(String message) {
        if (message == null) {
            throw new BizException("");
        }
        throw new BizException(message);
    }

    static String format(String message, Object expected, Object actual) {
        String formatted = "";
        if (message != null && !message.equals("")) {
            formatted = message + " ";
        }
        String expectedString = String.valueOf(expected);
        String actualString = String.valueOf(actual);
        if (expectedString.equals(actualString)) {
            return formatted + "expected: " + formatClassAndValue(expected, expectedString) + " but was: " + formatClassAndValue(actual, actualString);
        } else {
            return formatted + "expected:<" + expectedString + "> but was:<" + actualString + ">";
        }
    }

    private static String formatClassAndValue(Object value, String valueString) {
        String className = value == null ? "null" : value.getClass().getName();
        return className + "<" + valueString + ">";
    }

    public static void notNull(Object obj) {
        notNull(obj, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notNull(Object obj, String msg) {
        notNull(obj, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notNull(Object obj, ErrorEnum errorEnum) {
        notNull(obj, errorEnum, null);
    }

    public static void notNull(Object obj, ErrorEnum errorEnum, String msg) {
        if (obj == null) {
            if (StringUtils.isBlank(msg)) {
                throw new BizException(errorEnum);
            }
            throw new BizException(errorEnum, msg);
        }
    }

    public static void isNull(Object obj) {
        isNull(obj, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void isNull(Object obj, String msg) {
        isNull(obj, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void isNull(Object obj, ErrorEnum errorEnum, String msg) {
        if (obj != null) {
            throw new BizException(errorEnum, msg);
        }
    }

    public static void notBlank(String str) {
        notBlank(str, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notBlank(String str, String msg) {
        notBlank(str, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notBlank(String str, ErrorEnum errorEnum) {
        notBlank(str, errorEnum, null);
    }

    public static void notBlank(String str, ErrorEnum errorEnum, String msg) {
        if (StringUtils.isBlank(str)) {
            if (StringUtils.isBlank(msg)) {
                throw new BizException(errorEnum);
            }
            throw new BizException(errorEnum, msg);
        }
    }

    public static void notEmpty(Collection<?> list) {
        notEmpty(list, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notEmpty(Collection<?> list, String msg) {
        notEmpty(list, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notEmpty(Collection<?> list, ErrorEnum errorEnum, String msg) {
        if (list == null || list.isEmpty()) {
            throw new BizException(errorEnum, msg);
        }
    }

    public static void notEmpty(Object[] array) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notEmpty(Object[] array, String msg) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notEmpty(Object[] array, ErrorEnum errorEnum, String msg) {
        if (array == null || array.length > 0) {
            throw new BizException(errorEnum, msg);
        }
    }

    public static void notEmpty(byte[] array) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notEmpty(byte[] array, String msg) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notEmpty(byte[] array, ErrorEnum errorEnum, String msg) {
        if (array == null || array.length > 0) {
            throw new BizException(errorEnum, msg);
        }
    }

    public static void notEmpty(int[] array) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notEmpty(int[] array, String msg) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notEmpty(int[] array, ErrorEnum errorEnum, String msg) {
        if (array == null || array.length > 0) {
            throw new BizException(errorEnum, msg);
        }
    }

    public static void notEmpty(char[] array) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void notEmpty(char[] array, String msg) {
        notEmpty(array, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void notEmpty(char[] array, ErrorEnum errorEnum, String msg) {
        if (array == null || array.length > 0) {
            throw new BizException(errorEnum, msg);
        }
    }

    public static void greaterThan(Number number, Number compareNum) {
        greaterThan(number, compareNum, ErrorEnum.INVALID_PARAMS.getDesc());
    }

    public static void greaterThan(Number number, Number compareNum, String msg) {
        greaterThan(number, compareNum, ErrorEnum.INVALID_PARAMS, msg);
    }

    public static void greaterThan(Number number, Number compareNum, ErrorEnum errorEnum, String msg) {
        if (compareNum == null) {
            return;
        }
        notNull(number, errorEnum, msg);
        if (number.doubleValue() <= compareNum.doubleValue()) {
            throw new BizException(errorEnum, msg);
        }
    }

}
